window.onload = function () {
  window.zjsdk3 = new ZjSdk3({
    user_mcu_host: demo_config.server.ip
  });
  window.zjsdk3.user_mcu_host = demo_config.server.ip
  console.log('window.zjsdk3', window.zjsdk3)
  // 重写回调函数
  // zjsdk3.on_dev_call_succeed = demo_on_dev_call_succeed
  zjsdk3.on_dev_force_to_submr = on_dev_force_to_submr
  zjsdk3.on_dev_suggest_to_submrs = on_dev_suggest_to_submrs
  zjsdk3.on_dev_test_device_failed = demo_on_dev_test_device_failed
  zjsdk3.demo_on_dev_device_added = demo_on_dev_device_added

  window.loginMgr = new LoginMgr(demo_config.server.ip, demo_config.server.port, {
    dev_id: 'bc22efa9-ae37-11ef-b2a4-000c2989851e',
    dev_token: 'bc22efb4-ae37-11ef-b2a4-000c2989851e'
  })
  loginMgr.on_receive_invite = on_receive_invite
  console.log(loginMgr)
}

// 获取当前的本地设备
function demo_on_dev_device_added(device_id, label, kind, is_default) {
  console.log('---------------------- demo_on_dev_device_added', device_id, label, kind, is_default)
  //获取摄像头列表
  if (kind == 'videoinput') {
    vidoObj = {
      device_id: device_id, //设备id
      label: label //设备名称
    }
    videoLsit.push(vidoObj) //存放摄像头设备的list
    //获取摄像头设备列表
    let select = document.getElementById('videoDevices')
    //获取切换视频源的设备列表
    let selects = document.getElementById('overVideo')
    select.options.length = 0
    selects.options.length = 0
    videoLsit.forEach((item) => {
      select.options.add(new Option(item.label, item.device_id));
      selects.options.add(new Option(item.label, item.device_id));
    })
  } else if (kind == 'audioinput') {
    audioObj = {
      device_id: device_id, //设备id
      label: label //设备名称
    }
    audioList.push(audioObj) //存放麦克风设备的lsit
    // 获取麦克风设备列表
    let select = document.getElementById('audioDevices')
    //获取切换音频源的设备列表
    let selects = document.getElementById('overAudio')
    select.options.length = 0
    selects.options.length = 0
    audioList.forEach((item) => {
      select.options.add(new Option(item.label, item.device_id));
      selects.options.add(new Option(item.label, item.device_id));
    })
  }
}

// 入会邀请消息通知
function on_receive_invite(data) {
  console.log('---------------------- on_dev_force_to_submr', data)
}

// 测试摄像头/麦克风失败的回调函数。
function demo_on_dev_test_device_failed(is_camera,device_id,reason) {
  console.log('---------------------- on_dev_test_device_failed', is_camera,device_id,reason)
}

// 平台强制端进子会议
function on_dev_force_to_submr(sub_cuid, sub_name) {
  console.log('---------------------- on_dev_force_to_submr', sub_cuid, sub_name)
}

// 平台通知端子会议列表
function on_dev_suggest_to_submrs(sub_cuids, sub_names, ind_only) {
  console.log('---------------------- on_dev_suggest_to_submrs', sub_cuids, sub_names, ind_only)
}

const roomList = []


const loginBtn = document.querySelector('.login')
const initBtn = document.querySelector('.init')
const putBtn = document.querySelector('.put')
const getInfoBtn = document.querySelector('.get-info')
const get_public_rooms = document.querySelector('.get-public-rooms')
const reject_join_mr = document.querySelector('.reject-join-mr')
const show_remote_video = document.querySelector('.show-remote-video')
const make_call_now = document.querySelector('.make-call-now')
const show_video_by_content = document.querySelector('.show-video-by-content')
const preview_dev_open_micphone = document.querySelector('.preview-dev-open-micphone')
const add_schedule = document.querySelector('.add-schedule')
const preview_dev_play_audio = document.querySelector('.preview-dev-play-audio')
const preview_dev_open_camera = document.querySelector('.preview-dev-open-camera')
const localVideo = document.querySelector('.local-video')
const logout = document.querySelector('.logout')

// 登录
loginBtn.addEventListener('click', e => {
  loginMgr.login('dqyptyg4', '51vmr.com', '1234').then(res => {
    console.log('---------------------- res', res)
    loginMgr.init()
  })
  
})

// 退出登录
logout.addEventListener('click', e => {
  loginMgr.logout()
})

// 上报端的状态
initBtn.addEventListener('click', e => {
  loginMgr.put_self_status('1')
})

// 获取个人信息
getInfoBtn.addEventListener('click', e => {
  console.log('123')
  loginMgr.get_self_info().then(res => {
    console.log('---------------------- get_self_info', res)
  })
})

// 获取可用公共云会议室列表
get_public_rooms.addEventListener('click', e => {
  loginMgr.get_public_rooms({cmdid: 'search_room', filter_type: ['disable', 'is_used'], filter_value:[0, 1]}).then(res => {
    roomList.splice(0)
    roomList.push(...res)
    console.log('---------------------- get_public_rooms', res)
  })
})

// 拒绝入会邀请
reject_join_mr.addEventListener('click', e => {
  loginMgr.reject_join_mr('bc22efa9-ae37-11ef-b2a4-000c2989851e').then(res => {
    console.log('---------------------- reject_join_mr', res)
  })
})

// 按照平台推荐顺序展示视频(远端视频(摄像头内容))
show_remote_video.addEventListener('click', e => {
  zjsdk3.show_video_by_orders(['remote-video'])
})

// 将接收到的双流展示到指定元素(远端共享内容)
show_video_by_content.addEventListener('click', e => {
  zjsdk3.show_video_by_content('remote-share')
})

// 呼入会议
make_call_now.addEventListener('click', async e => {
  await zjsdk3.make_call_now(demo_config.server.ip, demo_config.server.port, demo_config.meeting.cuid, demo_config.meeting.joinpwd, demo_config.meeting.nick_name, demo_config.meeting.account)
})

// 麦克风测试
preview_dev_open_micphone.addEventListener('click', e => {
  zjsdk3.preview_dev_open_micphone('default', (mic_energy) => {
    console.log('当前声音', mic_energy)
  })
})

// 扬声器测试
preview_dev_play_audio.addEventListener('click', e => {
  zjsdk3.preview_dev_play_audio('default', '0')
})

// 新建预约
add_schedule.addEventListener('click', e => {
  loginMgr.add_schedule({
    "auto_invite": 1,
    "emdid": "add_schedule",
    "ctrl_pwd": "2222",
    "duration": 11040,
    "extra": "4444",
    "invite_usr ids":[71],
    "join_pwd": "3333",
    "mr_name": "test",
    "room_id": roomList[0].room_id,
    "start_dtm": 1742774920,
    "weekly_period": "0"
  })
})

// 摄像头预览
preview_dev_open_camera.addEventListener('click', async e => {
  const res = await zjsdk3.preview_dev_open_camera('default')
  console.log(res)
  localVideo.srcObject = res
  const text = document.createElement('p')
  text.innerText = '当前摄像头状态：' + res
  document.body.appendChild(text)
})